// Magic Software, Inc.
// http://www.magic-software.com
// Copyright (c) 2000, All Rights Reserved
//
// Source code from Magic Software is supplied under the terms of a license
// agreement and may not be copied or disclosed except in accordance with the
// terms of that agreement.  The various license agreements may be found at
// the Magic Software web site.  This file is subject to the license
//
// FREE SOURCE CODE
// http://www.magic-software.com/License/free.pdf

#ifndef MGCTLIST_H
#define MGCTLIST_H

#include "MgcRTLib.h"

// The TList class represents a singly linked list with header node.  The
// class T is intended to be native data (int, float, char*, etc.).  All
// list insertions occur at the front of the list.  If the list values were
// dynamically allocated by the application, it is the application's
// responsibility to delete those values.  This can be done by iterating
// over the list using GetFirst/GetNext and explicitly deleting the values.
//
// To iterate over a list, use
//
//     TList<T> kList;  // already built list
//     T tValue;
//     bool bFound = kList.GetFirst(tValue);
//     while ( bFound )
//     {
//         // code to process tValue goes here...
//         bFound = kList.GetNext(tValue);
//     }

template <class T>
class MgcTList
{
public:
    // construction and destruction
    MgcTList ();
    ~MgcTList ();

    // element access
    unsigned int GetQuantity () const;
    void Add (T tValue);
    bool AddUnique (T tValue);  // adds only not already in list
    bool Remove (T tValue);
    bool RemoveFront (T& rtValue);
    void RemoveAll ();

    // linear traversal of list
    bool GetFirst (T& rtValue);
    bool GetNext (T& rtValue);

    // list operations
    void ReverseOrder ();
    bool Contains (T tValue) const;

protected:
    class Node
    {
    public:
        Node (T tValue, Node* pkNext)
        {
            m_tValue = tValue;
            m_pkNext = pkNext;
        }

        ~Node ()
        {
            delete m_pkNext;
        }

        T m_tValue;
        Node* m_pkNext;
    };

    unsigned int m_uiQuantity;
    Node* m_pkFront;

    // iterator for traversal
    Node* m_pkIterator;
};

#include "MgcTList.inl"

#endif
